#pragma once
#include "CXcpObjectBase.h"
#include "NoBaseClass.h"
#include "CXcpObjectThreadSafeAddRefPolicy.h"
#include "IPLMListener.h"


class HWTexture;
class CTiledSurface;
class ImageHardwareResources;
class HWTextureManager;


class ImageSurfaceWrapper :
	public CXcpObjectBase<NoBaseClass,CXcpObjectThreadSafeAddRefPolicy>,
	IPLMListener
{
public: 
	static HRESULT Create(CCoreServices * pCore, XBOOL mustKeepSoftwareSurface, ImageSurfaceWrapper * * ppImageSurface);

	ImageSurfaceWrapper(CCoreServices * pCore, XBOOL mustKeepSoftwareSurface);
	virtual ~ImageSurfaceWrapper();
	HWTexture * GetHardwareSurface();
	struct IPALSurface * GetSoftwareSurface();
	struct IPALAcceleratedBitmap * GetD2DBitmap();
	CTiledSurface * GetTiledSurface();
	XUINT32 GetWidth();
	XUINT32 GetHeight();
	XUINT32 GetDisplayWidth();
	XUINT32 GetDisplayHeight();
	XBOOL IsOpaque();
	XBOOL HasAnySurface();
	XBOOL MustKeepSystemMemory();
	void ResetSurfaces(XBOOL mustKeepSoftwareSurface);
	void SetSoftwareSurface(struct IPALSurface * pSoftwareSurface, XBOOL allowHardwareVideoSurface);
	void UseCachedHardwareResources(ImageHardwareResources * pCachedResources);
	void SetD2DBitmap(struct IPALAcceleratedBitmap * pD2DBitmap);
	void SetKeepSystemMemory();
	XBOOL HasHardwareResources();
	HRESULT StartTiling(XINT32 width, XINT32 height, XBOOL isOpaque);
	XBOOL IsTiled();
	HRESULT EnsureHardwareResources(HWTextureManager * pTextureManager, ImageHardwareResources * * ppResources);
	HRESULT UpdateSurfaceFromSoftware(HWTextureManager * pTextureManager);
	XBOOL HasLostHardwareResources();
	void ReleaseLostHardwareResources();
	virtual HRESULT OnSuspend();
	virtual HRESULT OnResume();
private: 
	void ReleaseSoftwareSurfaceIfAllowed();
	struct IPALSurface * GetSoftwareOrHardwareSurface();
	CCoreServices * m_pCoreNoRef;
	struct IPALSurface * m_pImageSurfaceSoftware;
	HWTexture * m_pImageSurfaceHardware;
	struct IPALAcceleratedBitmap * m_pD2DBitmap;
	CTiledSurface * m_pTiledSurface;
	XBOOL m_mustKeepSystemMemory:1;


};
